﻿using Gs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DemoReception
{
    /// <summary>
    /// Hotel.xaml 的交互逻辑
    /// </summary>
    public partial class Hotel : Window
    {
        private GsToast iToast;
        private bool iIsInit;
        private List<KeyValuePair<string, string>> iHotelList;
        public Hotel()
        {
            InitializeComponent();
            iToast = new GsToast();
            iToast.BindControl(this.Content as Grid);
            iIsInit = CHotelConnect.Init();
            this.Loaded += new RoutedEventHandler(Hotel_Loaded);
        }

        public void Hotel_Loaded(object sender, RoutedEventArgs e)
        {
            if (!iIsInit)
            {
                iToast.Show("获取酒店信息失败!");
                return;
            }
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                int Rst = CHotelController.GetHotelList(out iHotelList);
                if (Rst < 0)
                {
                    iToast.Show("获取酒店信息失败!");
                }
                if (iHotelList != null)
                {
                    InitHotel();
                }
            }));
        }

        public void InitHotel()
        {
            cbHotel.ItemsSource = null;
            cbHotel.ItemsSource = iHotelList;
            cbHotel.DisplayMemberPath = "Value";
            cbHotel.SelectedValuePath = "Key";
            if (iHotelList.Count > 0)
            {
                cbHotel.SelectedIndex = 0;
                tbCode.Text = iHotelList[0].Key;
                tbName.Text = iHotelList[0].Value;
            }
        }

        private void newBtn_Click(object sender, RoutedEventArgs e)
        {
            string Code = tbCode.Text.Trim();
            string Name = tbName.Text.Trim();
            if(string.IsNullOrEmpty(Code)||string.IsNullOrEmpty(Name))
            {
                iToast.Show("请填入完整酒店信息!");
                return;
            }
            this.Dispatcher.BeginInvoke(new Action(() =>
            {

                int Rst = CHotelController.NewHotel(Code, Name);
                if (Rst < 0)
                {
                    if (Rst == -2)
                    {
                        iToast.Show("酒店信息重复,新建失败!");
                    }
                    else
                    {
                        iToast.Show("新建酒店信息失败!");
                    }
                    return;
                }
                Rst = CHotelController.GetHotelList(out iHotelList);
                if (Rst < 0)
                {
                    iToast.Show("获取酒店信息失败!");
                    return;
                }
                if (iHotelList != null)
                {
                    InitHotel();
                }
            }));
        }

        private void loginBtn_Click(object sender, RoutedEventArgs e)
        {
            string HotelCode = cbHotel.SelectedValue.ToString();
            MainWindow RoomState = new MainWindow(HotelCode);
            RoomState.Show();
            this.Close();
        }

        private void cbHotel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int Index = cbHotel.SelectedIndex;
            if (Index < 0)
            {
                tbCode.Text = tbName.Text = string.Empty;
            }
            else
            {
                tbCode.Text = iHotelList[Index].Key;
                tbName.Text = iHotelList[Index].Value;
            }
        }
    }
}
